﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FitCalc.Models;

namespace FitCalc.Controllers
{
    public class CalculatorController : Controller
    {
        //
        // GET: /Calculator/
        private static PaceSpeedCalculatorModel _paceSpeed = new PaceSpeedCalculatorModel();


        public ActionResult Index()
        {
            //_paceSpeed.paceMinutes = "1";
            //_paceSpeed.paceSeconds = "0";

            return this.RazorView(_paceSpeed);
        }
        [HttpPost]
        public ActionResult Index(PaceSpeedCalculatorModel pscm)
        {
            _paceSpeed = pscm;


            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult PaceToSpeed(PaceSpeedCalculatorModel pscm)
        {
            if (pscm.speed == null)
                pscm.speed = RunningCalc.PaceToSpeed(new RunningTime(pscm.paceMinutes, pscm.paceSeconds)).ToString();
            else
            {
                double speed = 0;
                RunningTime rt = null;
                if (double.TryParse(pscm.speed, out speed))
                    rt = RunningCalc.SpeedToPace(speed);
                else
                    rt = new RunningTime();
                pscm.paceMinutes = rt.Minutes.ToString();
                pscm.paceSeconds = rt.Seconds.ToString();
            }
            return this.View(pscm);

        }
    }
}
